﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http.Filters;
using System.Web.Http.Controllers;
using CL.Tools.Common;
using System.Collections;
using WebAPI.Models;
using CL.WebAPI.Controllers;
using System.IO;
using CL.Enum.Common;

namespace CL.WebAPI.Filters
{
    public class VerifySecretAttribute : ActionFilterAttribute
    {
        private ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);


        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            //交互方式
            string methed = actionContext.Request.Method.Method;

            long UnixTime = 0;
            string Secret = string.Empty;
            //调用方法
            string actionName = actionContext.ActionDescriptor.ActionName;
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            if (methed == "GET")
                parameters = RequestInputStream.SetParameters(actionContext.RequestContext.Url.Request.RequestUri.Query.Replace("?", ""), ref UnixTime, ref Secret);
            else if (methed == "POST" || methed == "PUT")
                parameters = RequestInputStream.SetParameters(actionContext.Request.Content.ReadAsStreamAsync().Result, ref UnixTime, ref Secret);
            new Log("VerifySecret").Write(string.Format("[{0}]{1}参数:{2}", actionName, methed, Newtonsoft.Json.JsonConvert.SerializeObject(parameters)), true);
            new DataSecret().VerifySecret(UnixTime, Secret, parameters, methed);
            //校验参数
            if (DataSecret.VerifyRec == (int)ResultCode.Success && actionName != "ApplyInitiate")
                new DataSecret().VerifyParameters(parameters);
        }
    }
}